package com.info.zhiduoduo.weixin.service;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.repository.entity.commonproblem.CommonProblemEntity;
import com.info.zhiduoduo.repository.mapper.commonproblem.CommonProblemMapper;
import com.info.zhiduoduo.weixin.formbean.commonproblem.GetCommonProblemFormBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Trink丶
 */
@Slf4j
@Service
public class CommonProblemService {
    @Autowired
    private CommonProblemMapper commonProblemMapper;

    /**
     * 查询常见问题
     *
     * @param formBean reqdata中传入需要查询的类型
     * @return 返回值
     */
    public SmartResult getCommonProblemList(BaseSimpleFormBean<GetCommonProblemFormBean> formBean) {
        /*
          配置分页信息
         */
        int page = formBean.getPage();
        int rows = formBean.getRows();
        if (page <= 0) {
            return SmartResult.fail().setMsg("分页信息出现问题，请联系管理员");
        }
        int start = page * rows - rows;
        /*
          查询条件
         */
        GetCommonProblemFormBean condition = formBean.getReqdata();
        Integer type = condition.getType();
        String msg = condition.getMsg();
        EntityWrapper<CommonProblemEntity> wrapper = new EntityWrapper<CommonProblemEntity>();
        wrapper
                .eq(null != type, "problem_type", type)
                .like(null != msg && msg.trim().length() != 0, "problem_description", msg,SqlLike.DEFAULT)
                .or(null != msg && msg.trim().length() != 0,"")
                .like(null != msg && msg.trim().length() != 0, "problem_respond", msg, SqlLike.DEFAULT);
        /*
          查询
         */
        List<CommonProblemEntity> result = commonProblemMapper.selectPage(new RowBounds(start, rows), wrapper);
        return SmartResult.success().setList(result);
    }
}
